<template>
	<view class="uni-container">
		<uni-card>
			<view style="height:540px;">
				<view>
					<zb-table :columns="column" :stripe="true" :border="true" :data="tableDatas"></zb-table>
				</view>
				<view style="margin-top: 10upx;margin-bottom: 10upx;">
					<uni-forms ref="Form" :modelValue="formData" :rules="formDataRules">
						<view class="row-parent row-item">
							<view class="row-style ">
								<view class="flex-twice radius">
									<uni-forms-item label="发现人员:" name="findRenYuan" labelWidth="80">
										<uni-easyinput type="text" v-model="formData.findRenYuan"
											placeholder="请输入发现人员" />
									</uni-forms-item>
								</view>
							</view>
							<view class="row-style " style="margin-left: 8upx;">
								<view class="flex-twice radius">
									<uni-forms-item label="发现日期:" name="findDate" labelWidth="80">
										<uni-easyinput type="text" v-model="formData.findDate" placeholder="请输入发现时间" />
									</uni-forms-item>
								</view>
							</view>
							<view class="row-style " style="margin-left: 8upx;">
								<view class="flex-twice radius">
									<uni-forms-item label="不良总数:" name="buliangshu" labelWidth="80">
										<uni-easyinput type="text" v-model="formData.buliangshu"
											placeholder="请输入不良总数" />
									</uni-forms-item>
								</view>
							</view>
							<view class="row-style " style="margin-left: 8upx;">
								<view class="flex-twice radius">
									<uni-forms-item label="工艺人员:" name="findgongyi" labelWidth="80">
										<uni-easyinput type="text" v-model="formData.findgongyi"
											placeholder="请输入工艺人员" />
									</uni-forms-item>
								</view>
							</view>
						</view>
						<view class="row-parent row-item" style="margin-top: 30upx;">
							<view class="row-style ">
								<view class="flex-twice radius">
									<uni-forms-item label="检验区域:" name="quyu" labelWidth="80">
										<uni-easyinput type="text" v-model="formData.quyu" placeholder="请输入检验区域" />
									</uni-forms-item>
								</view>
							</view>
							<view class="row-style " style="margin-left: 8upx;">
								<view class="flex-twice radius">
									<uni-forms-item label="加工设备:" name="shebei" labelWidth="80">
										<uni-easyinput type="text" v-model="formData.shebei" placeholder="请输入加工设备" />
									</uni-forms-item>
								</view>
							</view>
							<view class="row-style " style="margin-left: 8upx;">
								<view class="flex-twice radius">
									<uni-forms-item label="是否批量:" name="findIsFirst" labelWidth="80">
										<uni-data-select v-model="formData.findIsFirst" :localdata="findIsFirstData">
										</uni-data-select>
									</uni-forms-item>
								</view>
							</view>
							<view class="row-style " style="margin-left: 8upx;">
								<view class="flex-twice radius">
									<!-- <uni-forms-item label="是否批量:" name="name" labelWidth="80">
									<uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" />
								</uni-forms-item> -->
								</view>
							</view>
						</view>
					</uni-forms>
				</view>
				<view class="uni-group" style="margin-left:75%;">
					<button style="margin-right: 10upx;" class="uni-button" size="mini" type="primary" @click="addtable">添加</button>
					<button style="margin-right: 10upx;" class="uni-button" size="mini" type="primary" @click="saveallput">直接推送</button>
					<button class="uni-button" size="mini" type="primary" @click="saveall">保存</button>	
				</view>

				<uni-table ref="table" :loading="loading" border stripe emptyText="暂无更多数据"
					@selection-change="selectionChange">
					<uni-tr>
						<uni-th align="center">检验项</uni-th>
						<uni-th align="center">标准值</uni-th>
						<uni-th align="center">实测值</uni-th>
						<uni-th align="center">异常类型</uni-th>
						<uni-th align="center">测量描述</uni-th>
						<uni-th align="center">不良数</uni-th>
						<uni-th align="center">序列号/刻字号</uni-th>
						<uni-th align="center">操作员</uni-th>
						<uni-th align="center">设备标号型号</uni-th>
						<uni-th align="center">现场标号</uni-th>
						<uni-th align="center">装夹方式</uni-th>
						<uni-th align="center">工艺资源组</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in tableData" :key="index">
						<uni-td>{{ item.fkXc}}</uni-td>
						<uni-td>{{ item.chitStandardvalue}} | {{ item.chitPositiveerror}} |
							{{ item.chitNegativeerror}}</uni-td>
						<uni-td>{{ item.fkBzd}}</uni-td>
						<uni-td>{{ item.DescriptionSelectText}}</uni-td>
						<uni-td>{{ item.fkCcz}}</uni-td>
						<uni-td>{{ item.fkBls}}</uni-td>
						<uni-td>{{ item.fkXlh}}</uni-td>
						<uni-td>{{ item.czy}}</uni-td>
						<uni-td>{{ item.sbbh}}</uni-td>
						<uni-td>{{ item.xcbh}}</uni-td>
						<uni-td>{{ item.zjfsText}}</uni-td>
						<uni-td>{{ item.processResourcesGroupText}}</uni-td>
					</uni-tr>
				</uni-table>
				<view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent"
						:total="total" @change="change" /></view>
				<view>
					<!-- 输入框示例 -->
					<!-- 		<uni-popup ref="inputDialog" type="dialog">
						<uni-popup-dialog ref="inputClose" mode="input" title="异常信息" value="" :before-close="true"
							placeholder="请输入完工数" @confirm="dialogInputConfirm" @close="dialogInputConfirmclose">
							
						</uni-popup-dialog>
					</uni-popup> -->
				</view>

			</view>
		</uni-card>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" type="message">
				<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		allList,
		addfankuiIfon,
		listByCode,
	} from '@/api/system/fankui'
	export default {
		data() {
			return {
				// 每页数据量
				pageSize: 5,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				msgType: '',
				messageText: '',
				formData: {
					findRenYuan: "",
					findDate: this.NyrTime(),
					buliangshu: "",
					findgongyi: "",
					quyu: "",
					shebei: "",
					findIsFirst: "",
				}, //固定表格
				tableFormData: {
					"FeatureCode": "",
					"Value": "",
					"Description": "",
					"ErrorCount": "",
					"SerialNo": "",
					"czy": "",
					"sbbh": "",
					"xcbh": "",
					"zjfs": "",
				},
				loading: false,
				tableData: [],
				tableDataG: [],
				tableDatas: [],
				pushtableData: [],
				pushtableDataput: [],
				workItem: {},
				selectedIndexsValue: [],
				formDataRules: {
					findRenYuan: {
						rules: [{
							required: true,
							errorMessage: '发现人员不能为空'
						}]
					},
					findDate: {
						rules: [{
							required: true,
							errorMessage: '发现时间不能为空'
						}]
					},
					buliangshu: {
						rules: [{
							required: true,
							errorMessage: '不良总数不能为空'
						}, {
							format: 'number',
							errorMessage: '不良总数只能输入数字'
						}]
					},
					shebei: {
						rules: [{
							required: true,
							errorMessage: '加工设备不能为空'
						}]
					},
					findIsFirst: {
						rules: [{
							required: true,
							errorMessage: '是否批量不能为空'
						}]
					},
				},
				findIsFirstData: [],
				tableRules: {
					ErrorCount: {
						rules: [{
							required: true,
							errorMessage: '不良数不能为空'
						}, {
							format: 'number',
							errorMessage: '不良数只能输入数字'
						}]
					},
				},
				column: [{
						label: "MO号",
						name: "mo",
						width: 140
					},
					{
						label: "工序号",
						name: "jobCode",
					},
					{
						label: "工序名称",
						name: "jobName",
					},
					{
						label: "数量",
						name: "jobPlanNum",
					},
					{
						label: "料号",
						name: "mateCode",
						width: 120
					},
					{
						label: "图号",
						name: "th",
					},
					{
						label: "版本号",
						name: "thVersion",
					},

					{
						label: "加工设备",
						name: "jobResourname",
						width: 190
					},

					{
						label: "状态",
						name: "stutStatename",
					}
				],
			};

		},
		onLoad(params) {
			console.log(params.parme)
			this.formData.findRenYuan = JSON.parse(params.parme).findRenYuan;
			this.formData.buliangshu = JSON.parse(params.parme).jobPlanNum;
			this.formData.shebei = JSON.parse(params.parme).jobResourname;
			this.workItem = JSON.parse(params.parme);
			this.tableDatas.push(JSON.parse(params.parme));
			this.allListtable();
			this.onfindIsFirst();
			this.selectedIndexs = []
			this.selectedIndexsValue = []
			
		},
		onShow() {
			let param = uni.getStorageSync('param') || {} // 从本地存储中获取参数
			//console.log("param", param,Object.keys(param).length === 0)
			if(Object.keys(param).length != 0){
				this.tableDataG.push({
					"fkXc": param.FeatureCode,
					"fkBzd": param.Value,
					"fkCcz": param.Description,
					"fkBls": param.ErrorCount,
					"fkXlh": param.SerialNo,
					"czy": param.czy,
					"sbbh": param.sbbh,
					"xcbh": param.xcbh,
					"zjfsText": param.zjfsText,
					"chitStandardvalue": param.chitStandardvalue,
					"chitPositiveerror": param.chitPositiveerror,
					"chitNegativeerror": param.chitNegativeerror,
					"chitValuetype": param.chitValuetype,
					"DescriptionSelectText": param.DescriptionSelectText,
					"fileUrlList": param.fileList,
					"shebei": param.shebei,
					"processResourcesGroupText": param.processResourcesGroupText,
				});
				this.pushtableDataput.push({
					"fkXc": param.FeatureCode,
					"fkBzd": param.Value,
					"fkCcz": param.Description,
					"fkBls": param.ErrorCount,
					"fkXlh": param.SerialNo,
					"czy": param.czy,
					"sbbh": param.sbbh,
					"xcbh": param.xcbh,
					"zjfs": param.zjfs,
					"chitStandardvalue": param.chitStandardvalue,
					"chitPositiveerror": param.chitPositiveerror,
					"chitNegativeerror": param.chitNegativeerror,
					"chitValuetype": param.chitValuetype,
					"DescriptionSelect": param.DescriptionSelect,
					"fileUrlList": param.fileList,
					"shebei": param.shebei,
					"processResourcesGroup": param.processResourcesGroup,
					"zjfsText": param.zjfsText,
					"processResourcesGroupText": param.processResourcesGroupText,
					"DescriptionSelectText": param.DescriptionSelectText,
					"pushNow": 1,
				});
				this.pushtableData.push({
					"fkXc": param.FeatureCode,
					"fkBzd": param.Value,
					"fkCcz": param.Description,
					"fkBls": param.ErrorCount,
					"fkXlh": param.SerialNo,
					"czy": param.czy,
					"sbbh": param.sbbh,
					"xcbh": param.xcbh,
					"zjfs": param.zjfs,
					"chitStandardvalue": param.chitStandardvalue,
					"chitPositiveerror": param.chitPositiveerror,
					"chitNegativeerror": param.chitNegativeerror,
					"chitValuetype": param.chitValuetype,
					"DescriptionSelect": param.DescriptionSelect,
					"fileUrlList": param.fileList,
					"shebei": param.shebei,
					"processResourcesGroup": param.processResourcesGroup,
					"zjfsText": param.zjfsText,
					"processResourcesGroupText": param.processResourcesGroupText,
					"DescriptionSelectText": param.DescriptionSelectText,
					"pushNow": 0,
				});
			}
			uni.setStorage({
			  key: 'param', // 自定义参数名称
			  data: {}, // 参数对象
			})
			var that = this;
			setTimeout(function() {
			   that.getData(1);
			}, 500);
		    
		},
		methods: {
			//是否首件 
			onfindIsFirst(){
				listByCode("FIRST_PIECE_IDENTIFICATION").then((res) => {
					console.log("FIRST_PIECE_IDENTIFICATION",res.data)
				    for (var i = 0; i < res.data.length; i++) {
				    	let d = {
				    		text: res.data[i].dictName,
				    		value: res.data[i].dictCode,
				    	}
				    	this.findIsFirstData.push(d)
				    }
				});
			},
			// 分页触发
			change(e) {
				this.getData(e.current)
			},
			// 获取数据
			getData(pageCurrent, value = '') {
				this.loading = true
				this.pageCurrent = pageCurrent
				var a = {
					pageSize: this.pageSize,
					pageCurrent: pageCurrent,
					value: value,
					success: res => {
						console.log('data', res);
						this.tableData = res.data
						this.total = res.total
						this.loading = false

					}
				}
				console.log(a)
				this.request(a)
			},
			// 伪request请求
			request(options) {
				const {
					pageSize,
					pageCurrent,
					success,
					value
				} = options
				let total = this.tableDataG.length
				let data = this.tableDataG.filter((item, index) => {
					const idx = index - (pageCurrent - 1) * pageSize
					return idx < pageSize && idx >= 0
				})
				// if (value) {
				// 	data = []
				// 	this.tableData.forEach(item => {
				// 		if (item.name.indexOf(value) !== -1) {
				// 			data.push(item)
				// 		}
				// 	})
				// 	total = data.length
				// }

				setTimeout(() => {
					typeof success === 'function' &&
						success({
							data: data,
							total: total
						})
				}, 500)

			},

			//反馈信息表格
			allListtable() {
				console.log(this.workItem.jobId)
				allList(this.workItem.jobId).then(res => {
					this.tableDataG = res.data;
					console.log("反馈信息表格", res)
					this.getData(1)
				}).catch(error => {
					reject(error)
				})
			},
			//获取年月日
			NyrTime() {
				const date = new Date();
				var year = date.getFullYear(); // 获取当前年份，例如：2021
				var month = date.getMonth() + 1; // 获取当前月份，注意需要加1，例如：9
				var day = date.getDate(); // 获取当前日期，例如：2
				var hours = date.getHours();
				var minutes = date.getMinutes();
				var seconds = date.getSeconds();
				month = month < 10 ? '0' + month : month;
				day = day < 10 ? '0' + day : day;
				hours = hours < 10 ? '0' + hours : hours;
				minutes = minutes < 10 ? '0' + minutes : minutes;
				seconds = seconds < 10 ? '0' + seconds : seconds;
				const time = year + ":" + month + ":" + day + ":" + minutes + ":" + seconds
				return time;
			},
			// 多选行
			selectionChange(e) {
				this.selectedIndexsValue = JSON.parse(JSON.stringify(e.detail.index));
			},
			// 删除行
			deletetable() {
				console.log(this.selectedIndexsValue)
				for (var i = this.selectedIndexsValue.length - 1; i >= 0; i--) {
					this.tableData.splice(this.selectedIndexsValue[i], 1)
				}
				this.$refs.table.clearSelection()
			},
			// 添加行
			addtable() {
				// this.$refs.inputDialog.open()
				// this.$refs.table.clearSelection()
				uni.navigateTo({
					url: '/pages/work/add?parme=' + JSON.stringify(this.workItem)
				})
			},
            saveallput() {
				this.$refs["Form"].validate().then(res => {
					var data = this.pushtableDataput;
					let params = {
						jobId:this.workItem.jobId,
						patycode: this.workItem.patycode,
						equiId: this.workItem.jobResourId,
						findRenYuan: this.formData.findRenYuan,
						findDate: this.formData.findDate,
						findIsFirst: this.formData.findIsFirst,
						shebei: this.formData.shebei,
						quyu: this.formData.quyu,
						findgongyi: this.formData.findgongyi,
						buliangshu: this.formData.buliangshu,
						data: data,
						systemCode: "NEW",
					};
					console.log(params)
					addfankuiIfon(params).then(res => {
						console.log(res)
						if (res.code == 200) {
				
							this.pushtableDataput = [];
							this.formData = {
								findRenYuan: this.workItem.findRenYuan,
								findDate: this.NyrTime(),
								buliangshu: this.workItem.jobPlanNum,
								findgongyi: "",
								quyu: "",
								shebei: "",
								findIsFirst: "",
							};
							this.allListtable();
							this.$modal.msgSuccess("保存成功")
						} else {
							this.msgType = "error"
							this.messageText = res.msg
							this.$refs.message.open()
						}
					});
				}).catch(err => {
					console.log('err', err);
				})
			},
			saveall() {
				this.$refs["Form"].validate().then(res => {
					var data = this.pushtableData;
					let params = {
						jobId:this.workItem.jobId,
						patycode: this.workItem.patycode,
						equiId: this.workItem.jobResourId,
						findRenYuan: this.formData.findRenYuan,
						findDate: this.formData.findDate,
						findIsFirst: this.formData.findIsFirst,
						shebei: this.formData.shebei,
						quyu: this.formData.quyu,
						findgongyi: this.formData.findgongyi,
						buliangshu: this.formData.buliangshu,
						data: data,
						systemCode: "NEW",
					};
					console.log(params)
					addfankuiIfon(params).then(res => {
						console.log(res)
						if (res.code == 200) {

							this.pushtableData = [];
							this.formData = {
								findRenYuan: this.workItem.findRenYuan,
								findDate: this.NyrTime(),
								buliangshu: this.workItem.jobPlanNum,
								findgongyi: "",
								quyu: "",
								shebei: "",
								findIsFirst: "",
							};
							this.allListtable();
							this.$modal.msgSuccess("保存成功")
						} else {
							this.msgType = "error"
							this.messageText = res.msg
							this.$refs.message.open()
						}
					});
				}).catch(err => {
					console.log('err', err);
				})
			}

		}
	};
</script>

<style>
	::v-deep {
		.uni-card {
			margin: 8px !important;
			padding: 0 !important;

			.uni-card__content {
				padding: 0 !important;
			}
		}
	}

	.row-parent {
		width: 80upx;
		width: 100%;
	}

	.row-item {
		display: flex;
		flex-direction: row; //设置布局方向为水平
	}

	.row-style {
		text-align: center;
		width: 33.33%; //每个元素宽度占比父类的三分之一
		height: 80upx;
		line-height: 80upx;
	}

	/deep/.uni-popup .uni-popup__wrapper {
		width: 80% !important;
		padding: 40rpx;
	}

	/deep/.uni-popup-dialog[data-v-6f54520a] {
		width: 100% !important;

	}
</style>